{% extends 'frame.html' %}
{% block title %}Automagic{% endblock %}
{% block slideshow %}
    {% if request.user.is_admin %}
        <div class="ak-zper-midbox ak-jsch-mid">
            <div class="ak-zper-inbox-title ak-jsch-title clearfix">
                <div class="ak-left ak-jsch-row3 ak-zper-fix-btnbox">
                    <span class="ak-zper-remindback"></span>
                    <span class="inbox-text">关键字管理</span>
                </div>
                <div id="log_info" class="ac-addtips"></div>
            </div>

            <!--      ---------------------华丽的分割线 main------------------------ -->
            <div class="col-md-12">
                <div class="ac-margin-tb15">
                    <form method='post' id="add_keyword">
                        {% csrf_token %}
                        <table class="table table-condensed ac-aelement-table">
                            <tr>
                                <th>关键字</th>
                                <th>描述</th>
                                <th width="150px">关键字类型</th>
                                <th></th>
                            </tr>
                            <tr>
                                <td><input id="keyword" maxlength="100" class="form-control" placeholder="请输入需要添加的关键字缩写"
                                           name="keyword" required="" type="text"></td>
                                <td><input id="kwdescr" maxlength="200" class="form-control" placeholder="请输入关键字描述"
                                           name="kwdescr" type="text"></td>
                                <td>
                                    <select id="selproductid" name="productname" class="ak-right ak-left ac-aselect">
                                        <option value="0">通用</option>
                                        {% for product in userandproduct %}
                                            {% if product.username == request.user %}
                                                <option value="{{ product.productname.id }}"
                                                        {% if request.GET.check_productname  == product.productname.id %}selected{% endif %}>{{ product.productname.name }}</option>
                                            {% endif %}
                                        {% endfor %}
                                    </select>
                                </td>
                                <td>
                                    <input class="btn btn-sm btn-success" type="submit" value="添加">
                                    <input class="btn btn-sm btn-primary" type="reset" value="重置">
                                </td>
                            </tr>
                        </table>
                    </form>

                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <form class="form-inline" role="form" method="GET">
                                <div class="form-group">
                                    <div class="input-group">
                                        <input type="input" class="form-control search-width" name="keyword"
                                               value="{{ request.GET.keyword }}" placeholder="通过关键字或者描述搜索">
                                        <span class="input-group-btn">
                      <button class="btn btn-primary" type="submit">Go!</button>
                  </span>
                                    </div>
                                </div>
                            </form>
                        </div>

                        <table id="mytable" class="table table-striped table-hover table-condensed ac-aelement-table">
                            <thead>
                            <tr>
                                <th>ID.</th>
                                <th>关键字</th>
                                <th>描述</th>
                                <th>关键字类型</th>
                                <th>创建者</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for keyword in keywordlist %}
                                <tr>
                                    <td>{{ keyword.pk }}</td>
                                    <td>{{ keyword.keyword }}</td>
                                    <td>{{ keyword.kwdescr }}</td>
                                    <td>
                                        {% if keyword.productid > 0 %}
                                            {% for p in productlist %}
                                                {% if p.pk == keyword.productid %}
                                                    {{ p.name }}
                                                {% endif %}
                                            {% endfor %}
                                        {% else %}
                                            通用
                                        {% endif %}
                                    </td>
                                    <td>{{ keyword.createat }}</td>
                                    <td>{{ keyword.createtime | date:'Y-m-d H:i:s' }}</td>
                                    <td>
                                        <a href="" onclick="setkeywordValue({{ keyword.id }})" class="ke-ablock"
                                           data-toggle="modal" data-target="#editKeywordModal" role="button"><i
                                                class="glyphicon glyphicon-edit"></i></a>
                                        <a href="{% url 'keyworddel' keyword.id %}" class="ke-ablock" role="button"
                                           onclick="return confirm('您确定要删除吗？')"><i
                                                class="glyphicon glyphicon-trash"></i></a>
                                    </td>
                                </tr>
                            {% endfor %}

                            </tbody>
                        </table>
                    </div>
                    {% if is_paginated %}
                        <div class="panel-default">
                            <ul class="pagination">
                                <li>
                                    <a href="javascript:window.location.href=window.location.search.match(/page=(\d+|\s*)/)===null? window.location.origin+(window.location.pathname +'?page=1'+(window.location.search?window.location.search.replace('?','&'):'')):window.location.search.replace(/page=(\d+|\s*)/, 'page=1')">«</a>
                                </li>
                                {% if page_obj.has_previous %}
                                    <!--<li><a href="{{ request.get_full_path }}&page={{ page_obj.previous_page_number }}">上一页</a></li>-->
                                    <li>
                                        <a href="javascript:window.location.href=window.location.search.match(/page=(\d+|\s*)/)===null? window.location.href +'?page={{ page_obj.previous_page_number }}': window.location.search.replace(/page=(\d+|\s*)/, 'page={{ page_obj.previous_page_number }}')">上一页</a>
                                    </li>
                                {% else %}
                                    <li class="previous disabled"><a>上一页</a></li>
                                {% endif %}
                                {% for i in page_obj.paginator.page_range %}
                                    <li {% if page_obj.number == i %}class="active"{% endif %}><a
                                            href="javascript:window.location.href=window.location.search.match(/page=(\d+|\s*)/)===null? window.location.origin+(window.location.pathname +'?page={{ i }}'+(window.location.search?window.location.search.replace('?','&'):'')):window.location.search.replace(/page=(\d+|\s*)/, 'page={{ i }}')">{{ i }}</a>
                                    </li>
                                {% endfor %}
                                {% if page_obj.has_next %}
                                    <li>
                                        <a href="javascript:window.location.href=window.location.search.match(/page=(\d+|\s*)/)===null? window.location.origin+(window.location.pathname +'?page={{ page_obj.next_page_number }}'+(window.location.search?window.location.search.replace('?','&'):'')):window.location.search.replace(/page=(\d+|\s*)/, 'page={{ page_obj.next_page_number }}')">下一页</a>
                                    </li>
                                {% else %}
                                    <li class="previous disabled"><a>下一页</a></li>
                                {% endif %}
                                <li>
                                    <a href="javascript:window.location.href=window.location.search.match(/page=(\d+|\s*)/)===null? window.location.origin+(window.location.pathname +'?page={{ page_obj.paginator.num_pages }}'+(window.location.search?window.location.search.replace('?','&'):'')):window.location.search.replace(/page=(\d+|\s*)/, 'page={{ page_obj.paginator.num_pages }}')">»</a>
                                </li>
                                <li><a>当前第{{ page_obj.number }}页/共{{ keywordsum }}条</a></li>
                                <li><input id="curpage" class="ac-acaseedit-input" type="number" style="width: 80px;">
                                </li>
                                <li>
                                    <button id="btn_skip" onclick="btn_skip_click()" type="button"
                                            class="btn btn-sm btn-primary">确定
                                    </button>
                                </li>
                            </ul>
                        </div>
                    {% endif %}
                </div>
            </div>
            <!--      ---------------------华丽的分割线 main end------------------------ -->

        </div>
    {% else %}
        <div class="ak-zper-midbox ak-jsch-mid">
            <div class="ak-zper-inbox-title ak-jsch-title clearfix">
                <div class="ak-left ak-jsch-row3 ak-zper-fix-btnbox">
                    <span class="ak-zper-remindback"></span>
                    <span class="inbox-text">关键字管理</span>
                </div>
                <div id="log_info" class="ac-addtips"></div>
            </div>
            <div class="col-md-12">
                <div style="text-align: center">
                    <p>形成天才的决定因素应该是勤奋。</p>
                    <p>……有几分勤学苦练是成正比例的。</p>
                    <p>加油吧！少年！！！</p>
                    <p>你没权限访问，可以找找管理员。</p>
                </div>
            </div>
        </div>
    {% endif %}
    <!-- Edit Modal -->
    <div class="modal fade ak-min-fullwidth-modal ak-znews-addsort-modal" id="editKeywordModal" tabindex="-1"
         role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header clearfix">
                    <button type="button" class="close close-back" data-dismiss="modal"><span
                            class="sr-only">Close</span></button>
                    <h4 class="modal-title text-center">编辑关键字</h4>
                </div>
                <div class="modal-body col01 ac-amargin-tb20">
                    <form id="keyword_edit" method="post" action="{% url 'keywordupdate' %}" class="form-horizontal">

                        <div class="form-group">
                            <label class="col-lg-3 control-label">关键字</label>
                            <div class="col-lg-5">
                                <input name="keywordid" hidden="true">
                                <input type="text" class="form-control" name="keyword" placeholder="请填写关键字"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-lg-3 control-label">描述</label>
                            <div class="col-lg-5">
                                <input type="text" class="form-control" name="kwdescr" placeholder="描述"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 control-label">类型</label>
                            <div class="col-lg-5">
                                <select id="selproductid" name="productname" class="ak-left ac-aselect">
                                    <option value="0">通用</option>
                                    {% for product in userandproduct %}
                                        {% if product.username == request.user %}
                                            <option value="{{ product.productname.id }}"
                                                    {% if request.GET.check_productname  == product.productname.id %}selected{% endif %}>{{ product.productname.name }}</option>
                                        {% endif %}
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group modal-footer">
                            <div class="col-lg-9 col-lg-offset-3">
                                <!--===footer button ===-->
                                <div class="ak-footer-btn text-left">
                                    <input class="ak-zdl-createbtn" type="submit" value="提交">
                                    <input class="ak-zdl-createbtn cancel" type="reset" value="重置">
                                </div>
                                <!--===End footer button ===-->
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
{% block script %}
    {% load static %}
    <script>
        function btn_skip_click() {
            var currpage = $('#curpage').val() === '' ? 1 : parseInt($('#curpage').val());
            // console.log(currpage);
            if (currpage > {{ page_obj.paginator.num_pages }}) {
                currpage = {{ page_obj.paginator.num_pages }};
            }
            if (currpage < 1) {
                currpage = 1;
            }
            var url = window.location.search.match(/page=(\d+|\s*)/) === null ? window.location.origin + (window.location.pathname + '?page=' + currpage + (window.location.search ? window.location.search.replace('?', '&') : '')) : window.location.search.replace(/page=(\d+|\s*)/, 'page=' + currpage);
            self.location = url;

            return false;
        }

        $('#curpage').keypress(function (event) {
            console.log(event.which);
            if (event.which == 13) {
                btn_skip_click()
            }
            event.stopPropagation();
        })
    </script>
    <script type="text/javascript" src="{% static 'js/keyword.js' %}"></script>

{% endblock %}
